package com.example.qsmx.Util;

import java.io.*;
import java.sql.*;



// 将图片文件转成二进制的工具类
public class ImgToBinary {
    public static void storeImage(String userAccount, String imagePath) throws IOException, SQLException {
        // 读取图片文件到字节数组
        File imageFile = new File(imagePath);
        FileInputStream imageInFile = new FileInputStream(imageFile);
        byte[] imageBytes = new byte[(int) imageFile.length()];
        imageInFile.read(imageBytes);

        // 存储图片到数据库
        Connection connection1 = MyDBUtil.getConnection();

        String sql = "INSERT INTO useravatar (user_avatar_account, user_avatar_name, user_avatar_binary) VALUES (?, ?, ?)";
        PreparedStatement pstmt = connection1.prepareStatement(sql);
        pstmt.setString(1, userAccount);
        pstmt.setString(2, "userAvatar" + userAccount);
        pstmt.setBytes(3, imageBytes);
        pstmt.executeUpdate();

    }
}
